#include "tsina.h"

#include <boost/algorithm/string.hpp>
//#include <boost/regex.hpp>

#include <ctime>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/local_time/local_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

#include "ufs.h"
using namespace Ufs;
using namespace std;

using namespace Facebook;


string Facebook::gbk(const string& s)
{
     static CharsetConverter utf8_to_gbk("UTF-8", "GBK");
     string s1 = s;
     utf8_to_gbk.convert(s1);
     return s1;
}

string Facebook::utf8(const string& s)
{
     static CharsetConverter gbk_to_utf8("GBK", "UTF-8");
     string s1 = s;
     gbk_to_utf8.convert(s1);
     return s1;
}


void Facebook::displayStatus(const Status& s)
{
     cout << "========================================\n";
     cout << "user id: " <<  s.user.id << "   "
          << "userfollowing: " << s.user.friends_count << "   " 
          << "userfollowers: " << s.user.followers_count << "\n"
          << "screen_name: "<< gbk(s.user.screen_name) 
          << " description: " << gbk(s.user.description) << "\n"
          << "verified: "<< s.user.verified 
          << " gender: " << s.user.gender 
          << " user_created_ at: " << s.user.created_at << "\n";
 
     cout << "text: " << gbk(s.text) << endl;
     if (!s.original_pic.empty())
          cout << "image: " << s.original_pic << endl;
     
     cout << "created_at: " << s.created_at << endl;
     cout << "id: "<< s.id  << " "
          << "source: " << gbk(s.source) << endl; 
     if( s.retweeted_status.use_count() > 0 )
          cout << "retweeted_status: " << gbk(s.retweeted_status->text) << endl;
     
     cout << "========================================\n";
}



void Facebook::displayComment(const Comment& c)
{
     cout << "========================================\n";
     cout << "user id: " <<  c.user.id << "   "
          << "followers: " << c.user.followers_count << "\n"
          << "screen_name: "<< gbk(c.user.screen_name)
          << "description: " << gbk(c.user.description) << "\n"
          << "verified: "<< c.user.verified 
          << "gender: " << c.user.gender 
          << "user_created_ at: " << c.user.created_at << "\n";
     cout << "text: " << gbk(c.text) << endl;
     cout << "created_at: " << c.created_at << endl;
     cout << "id: "<< c.id  << " "
          << "source: " << c.source << endl;
     cout << "status: " << gbk(c.status.text) << endl;
     cout << "========================================\n";
}



void Facebook::displayUser(const User& user)
{
     cout << "========================================\n";
     cout << "user id: http://t.sina.com.cn/" <<  user.id << "\n"
          << "friends: " << user.friends_count << "\n"
          << "followers: " << user.followers_count << "\n"
          << "screen_name: "<< gbk(user.screen_name) << "\n"
          << "domain: "<< gbk(user.domain) << "\n"
          << "province: "<< gbk(user.province) << "\n"
          << "city: "<< gbk(user.city) << "\n"
          << "description: " << gbk(user.description) << "\n"
          << "url: " << user.url << "\n"
          << "verified: "<< user.verified << "\n"
          << "gender: " << user.gender << "\n"
          << "created_ at: " << user.created_at << "\n";

     if( user.status.use_count() > 0 )
     {
          cout << "status: " << gbk(user.status->text) << endl;
          cout << "status_id: " << user.status->id << endl;
          cout << "status_created_at: " << user.status->created_at << endl;
          cout << "status_source: " << gbk(user.status->source) << endl;
          cout << "  [" << (time(NULL) - getStdTime(user.status->created_at))/3600.0 << " hour(s) ago]";
          cout << "\n";
     }

     
     cout << "========================================\n";
}



// not so reliable
time_t Facebook::getStdTime(const string& created_at) 
{ 
     using namespace boost::gregorian;
     using namespace boost::local_time;
     using namespace boost::posix_time;


     if (!contains(created_at, "201")) return 0;
     
     try
     {
          local_time_input_facet* input_facet = new local_time_input_facet();
     
          stringstream ss(created_at);
    
          ss.imbue(locale(locale::classic(), input_facet));
          input_facet->format("%a %b %d %H:%M:%S +0800 %Y");
          local_date_time ldt(not_a_date_time);
          ss >> ldt;
    
          struct tm t = to_tm(ldt);
          time_t stdtime = mktime(&t);
	
          // delete input_facet;
          return  stdtime;
     }
     catch(...)
     {}

     return 0;
}







void Facebook::displayStatusShort(const Status& s)
{
     cout << gbk(s.user.screen_name)
          << "  http://t.sina.com.cn/" <<  s.user.id << "\n";
 
     cout << "text: " << gbk(s.text) << endl;
     //if (!s.original_pic.empty())   cout << "image: " << s.original_pic << endl;
     cout  << s.created_at << endl;
     cout << "id: "<< s.id  << "\n";
     // if( s.retweeted_status.use_count() > 0 )
     //     cout << "retweeted_status: " << gbk(s.retweeted_status->text) << endl;
}
